package com.huiyujinrong.qiangdanbao.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.huiyujinrong.qiangdanbao.R;
import com.huiyujinrong.qiangdanbao.activity.login.LoginActivity;
import com.huiyujinrong.qiangdanbao.utils.AppManager;
import com.huiyujinrong.qiangdanbao.utils.DataCleanHelper;
import com.huiyujinrong.qiangdanbao.utils.OSUtils;
import com.huiyujinrong.qiangdanbao.utils.SharedHelper;
import com.tencent.bugly.beta.Beta;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 设置
 * Created by ZQXQ-Developer on 2017/6/8.
 */

public class SettingActivity extends AppCompatActivity {

    private static final int CLEAN_CACHE = 100;
    @BindView(R.id.toolbar_Title)
    TextView tvTitle;

    @BindView(R.id.toolbar_Back)
    RelativeLayout rlBack;

    @BindView(R.id.tvVersions)
    TextView tvVsersions;//版本号

    @BindView(R.id.tvCache)
    TextView tvCache;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activitry_setting);
        ButterKnife.bind(this);

        setToolbar();

        tvVsersions.setText(OSUtils.getAPPVersion(this));

        tvCache.setText(DataCleanHelper.getTotalCacheSize(this));
    }


    /**
     * 设置标题栏
     */
    private void setToolbar() {
        tvTitle.setText("设置中心");
        rlBack.setVisibility(View.VISIBLE);
    }

    @OnClick({R.id.out_Login, R.id.toolbar_Back, R.id.rlCleanCache})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.out_Login:
                new AlertDialog.Builder(this)
                        .setCancelable(false)
                        .setMessage("确定退出登录?")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                SharedHelper.clear(SettingActivity.this);
                                /**
                                 * 在 退出登录时，关闭栈内所有activity  然后 跳转至loginActivity
                                 */
                                AppManager.getInstance().finishAllActivity();
                                Intent intent = new Intent(SettingActivity.this, LoginActivity.class);
                                intent.putExtra("isclean", true);
                                intent.putExtra("isBack", false);
                                startActivity(intent);
                                finish();

                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        })
                        .create()
                        .show()
                ;
                break;
            case R.id.toolbar_Back:
                finish();
                break;

            case R.id.rlCleanCache:
                //清除缓存
                showDialog();
                break;
        }
    }


    /**
     * 升级APK  全量升级
     */
    @OnClick(R.id.set_apk_update)
    void setAokUpdate() {
        Beta.checkUpgrade();
    }


    /**
     * 点击清楚缓存
     */
    private void showDialog() {
        final AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle("温馨提示").setMessage("是否清除缓存？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        toCleanCache();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                })
                .create();
        dialog.show();
    }

    /**
     * 清楚缓存
     */
    private void toCleanCache() {
        if (DataCleanHelper.clearAllCache(this)) {
            Toast.makeText(this, "清除成功", Toast.LENGTH_SHORT).show();
            tvCache.setText(DataCleanHelper.getTotalCacheSize(this));
        }
    }

}
